import { createRouter, createWebHistory, RouteRecordRaw, RouteLocationNormalized } from 'vue-router'
import { RouteMeta } from '@/types'
import { useAuthStore } from '@/store/auth'

// 定义路由
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    meta: { 
      requiresAuth: true,
      requiresLayout: true,
      title: '首页',
      icon: 'HomeFilled'
    } as RouteMeta
  },
  {
    path: '/order',
    name: 'Order',
    redirect: '/order/pricing-engine',
    meta: {
      requiresAuth: true,
      requiresLayout: true,
      title: '订单处理',
      icon: 'Document'
    } as RouteMeta,
    children: [
      {
        path: 'pricing-engine',
        name: 'PricingEngine',
        component: () => import('@/views/order/PricingEngine.vue'),
        meta: {
          requiresAuth: true,
          title: '智能定价引擎'
        } as RouteMeta
      },
      {
        path: 'delivery-calculation',
        name: 'DeliveryCalculation',
        component: () => import('@/views/order/DeliveryCalculation.vue'),
        meta: {
          requiresAuth: true,
          title: '交期承诺计算'
        } as RouteMeta
      },
      {
        path: 'change-tracking',
        name: 'ChangeTracking',
        component: () => import('@/views/order/ChangeTracking.vue'),
        meta: {
          requiresAuth: true,
          title: '订单变更溯源'
        } as RouteMeta
      }
    ]
  },
  {
    path: '/inventory',
    name: 'Inventory',
    redirect: '/inventory/warehouse/modeling',
    meta: {
      requiresAuth: true,
      requiresLayout: true,
      title: '库存管理',
      icon: 'Box'
    } as RouteMeta,
    children: [
      {
        path: 'warehouse',
        name: 'Warehouse',
        redirect: '/inventory/warehouse/modeling',
        meta: {
          requiresAuth: true,
          title: '仓储管理',
          icon: 'House'
        } as RouteMeta,
        children: [
          {
            path: 'modeling',
            name: 'WarehouseModeling',
            component: () => import('@/views/inventory/warehouse/Modeling.vue'),
            meta: {
              requiresAuth: true,
              title: '多仓库三维建模'
            } as RouteMeta
          },
          {
            path: 'location',
            name: 'IntelligentLocation',
            component: () => import('@/views/inventory/warehouse/Location.vue'),
            meta: {
              requiresAuth: true,
              title: '智能货位推荐'
            } as RouteMeta
          },
          {
            path: 'batch',
            name: 'BatchTracking',
            component: () => import('@/views/inventory/warehouse/Batch.vue'),
            meta: {
              requiresAuth: true,
              title: '批次/效期追踪'
            } as RouteMeta
          }
        ]
      },
      {
        path: 'control',
        name: 'InventoryControl',
        redirect: '/inventory/control/safety',
        meta: {
          requiresAuth: true,
          title: '库存控制',
          icon: 'TrendCharts'
        } as RouteMeta,
        children: [
          {
            path: 'safety',
            name: 'SafetyStock',
            component: () => import('@/views/inventory/control/Safety.vue'),
            meta: {
              requiresAuth: true,
              title: '动态安全库存计算'
            } as RouteMeta
          },
          {
            path: 'stagnant',
            name: 'StagnantWarning',
            component: () => import('@/views/inventory/control/Stagnant.vue'),
            meta: {
              requiresAuth: true,
              title: '呆滞料预警'
            } as RouteMeta
          },
          {
            path: 'turnover',
            name: 'TurnoverAnalysis',
            component: () => import('@/views/inventory/control/Turnover.vue'),
            meta: {
              requiresAuth: true,
              title: '库存周转分析'
            } as RouteMeta
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { 
      isPublic: true,
      requiresLayout: false,
      title: '登录'
    } as RouteMeta
  },
  {
    path: '/procurement',
    name: 'Procurement',
    redirect: '/procurement/supplier/classification',
    meta: {
      requiresAuth: true,
      requiresLayout: true,
      title: '采购管理',
      icon: 'ShoppingCart'
    } as RouteMeta,
    children: [
      {
        path: 'supplier',
        name: 'Supplier',
        redirect: '/procurement/supplier/classification',
        meta: {
          requiresAuth: true,
          title: '供应商管理',
          icon: 'Goods'
        } as RouteMeta,
        children: [
          {
            path: 'assessment',
            name: 'SupplierAssessment',
            component: () => import('@/views/procurement/supplier/Assessment.vue'),
            meta: {
              requiresAuth: true,
              title: '供应商准入评估体系'
            } as RouteMeta
          },
          {
            path: 'classification',
            name: 'SupplierClassification',
            component: () => import('@/views/procurement/supplier/Classification.vue'),
            meta: {
              requiresAuth: true,
              title: '分级分类管理'
            } as RouteMeta
          },
          {
            path: 'performance',
            name: 'SupplierPerformance',
            component: () => import('@/views/procurement/supplier/Performance.vue'),
            meta: {
              requiresAuth: true,
              title: '合作绩效看板'
            } as RouteMeta
          }
        ]
      },
      {
        path: 'process',
        name: 'ProcurementProcess',
        redirect: '/procurement/process/comparison',
        meta: {
          requiresAuth: true,
          title: '采购流程',
          icon: 'Connection'
        } as RouteMeta,
        children: [
          {
            path: 'comparison',
            name: 'PriceComparison',
            component: () => import('@/views/procurement/process/Comparison.vue'),
            meta: {
              requiresAuth: true,
              title: '智能比价系统'
            } as RouteMeta
          },
          {
            path: 'order',
            name: 'OrderLifecycle',
            component: () => import('@/views/procurement/process/Order.vue'),
            meta: {
              requiresAuth: true,
              title: '采购订单生命周期管理'
            } as RouteMeta
          },
          {
            path: 'monitoring',
            name: 'AnomalyMonitoring',
            component: () => import('@/views/procurement/process/Monitoring.vue'),
            meta: {
              requiresAuth: true,
              title: '异常采购监控'
            } as RouteMeta
          }
        ]
      }
    ]
  },
  {
    path: '/system',
    name: 'System',
    redirect: '/system/users',
    meta: {
      requiresAuth: true,
      requiresLayout: true,
      title: '系统管理',
      icon: 'Setting'
    } as RouteMeta,
    children: [
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/system/Users.vue'),
        meta: {
          requiresAuth: true,
          title: '用户管理'
        } as RouteMeta
      },
      {
        path: 'roles',
        name: 'Roles',
        component: () => import('@/views/system/Roles.vue'),
        meta: {
          requiresAuth: true,
          title: '角色管理'
        } as RouteMeta
      },
      {
        path: 'permissions',
        name: 'Permissions',
        component: () => import('@/views/system/Permissions.vue'),
        meta: {
          requiresAuth: true,
          title: '权限管理'
        } as RouteMeta
      }
    ]
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile.vue'),
    meta: {
      requiresAuth: true,
      requiresLayout: true,
      title: '个人中心',
      icon: 'UserFilled'
    } as RouteMeta
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    redirect: '/dashboard/overview',
    meta: {
      requiresAuth: true,
      requiresLayout: true,
      title: '监控大屏',
      icon: 'Monitor'
    } as RouteMeta,
    children: [
      {
        path: 'overview',
        name: 'DashboardOverview',
        component: () => import('@/views/dashboard/Overview.vue'),
        meta: {
          requiresAuth: true,
          title: '业务总览'
        } as RouteMeta
      },
      {
        path: 'inventory',
        name: 'DashboardInventory',
        component: () => import('@/views/dashboard/Inventory.vue'),
        meta: {
          requiresAuth: true,
          title: '库存分析'
        } as RouteMeta
      },
      {
        path: 'orders',
        name: 'DashboardOrders',
        component: () => import('@/views/dashboard/Orders.vue'),
        meta: {
          requiresAuth: true,
          title: '订单趋势'
        } as RouteMeta
      },
      {
        path: 'suppliers',
        name: 'DashboardSuppliers',
        component: () => import('@/views/dashboard/Suppliers.vue'),
        meta: {
          requiresAuth: true,
          title: '供应商绩效'
        } as RouteMeta
      }
    ]
  },
  {
    path: '/notifications',
    name: 'Notifications',
    component: () => import('@/views/Notifications.vue'),
    meta: {
      requiresAuth: true,
      requiresLayout: true,
      title: '通知中心',
      icon: 'Bell'
    } as RouteMeta
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: { 
      requiresLayout: false,
      title: '页面未找到'
    } as RouteMeta
  },
  // 通配符路由，匹配所有未定义的路由，重定向到404页面
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized) => {
  const auth = useAuthStore()
  
  // 需要认证但未登录 → 重定向登录页
  if (to.meta.requiresAuth && !auth.isAuthenticated) {
    return { path: '/login', query: { redirect: to.fullPath } }
  }
  
  // 已登录但访问登录页 → 重定向首页
  if (to.path === '/login' && auth.isAuthenticated) {
    return { path: '/' }
  }
})

export default router 